<?php

require_once  PLUGIN_PATH."/lib/adodb/adodb-exceptions.inc.php";
require_once  PLUGIN_PATH."/lib/adodb/adodb.inc.php";
require_once  PLUGIN_PATH."/lib/clean.php";


// define the database host machine - hostname or IP address
define('HQGISHOST',     '192.168.1.15');
// define the database name
define('HQGISDB',       'HQ_GIS');      
// define the database user
define('HQGISDBUSER',   'sa');       
// define the database password   
define('HQGISDBPASS',   'grabsem');
// define the database instance if necessary     
define('HQGISINSTANCE', '');            


class DBs {

        private static $masterDB = null;
        private static $marketDBs = array();
        private static $gisDB = null;       
        
	public static function closeMasterDB( ) {

                if( DBs::$masterDB != null ) {

                        DBs::$masterDB->Close();
                        DBs::$masterDB = null;  

                }

        }

//  create the connection to the database 
// since this code is running on linux and connecting to windows a special driver is needed
// in this instance the driver in use is ODBTP.  There are other options such as FreeTDS - you will have to config for your own setup
public static function getGISDB( ) {

                if( DBs::$gisDB == null ) {

                        $host = HQGISHOST;
                        if( HQGISINSTANCE ) {
                                $host .= "\\" . HQGISINSTANCE;
                        }                                     
                        $name = HQGISDB;                      
                        $user = HQGISDBUSER;                  
                        $pass = HQGISDBPASS;                  

                        $params = "DRIVER={SQL Server};SERVER=$host;UID=$user;PWD=$pass;DATABASE=$name;";
						
					// modify the connection parameter based on your linux DB driver
                        $db = ADONewConnection( 'odbtp' );                                                
                        $res = $db->Connect( HQGISHOST, $params );                                        
                        odbtp_convert_datetime();                                                         

                        if( $res == false ) {
                                throw new Exception( 'Failed to connect to GIS DB.' );
                        }                                                             

                        DBs::$gisDB = $db;
                }                         

                return DBs::$gisDB;

        }

        public static function closeGISDB( ) {

                if( DBs::$gisDB != null ) {

                        DBs::$gisDB->Close();
                        DBs::$gisDB = null;  

                }

        }


        public static function isDBOpen( $marketId=false ) {

                if( $marketId ) {

                        $marketId = Clean::int( $marketId );
                        $res = isset( DBs::$marketDBs[$marketId] );

                } else {

                        $res = (bool) DBs::$masterDB;

                }

                return $res;
        }

        public static function getTime( $db ) {

                if( $db == false ) {
                        throw new Exception( "Database undefined." );
                }

                $sql = "SELECT GETDATE()";
                $res = $db->GetOne( $sql );

                $res = strtotime( $res );
                if( $res === false ) {
                        throw new Exception( "Failed to retrieve time from database." );
                }

                return $res;

        }

}

?>

